package com.bxj.wxtt.controller;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import com.bxj.wxtt.domain.CacheParam;
import com.ifeng.matrix.cache.annotation.MatrixAddCache;
import com.ifeng.matrix.cache.components.MatrixCache;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;

/**
 * Created by baixiujiang on 17/10/15.
 */
@Controller
@RequestMapping(value = "/test")
@EnableScheduling
//@PropertySource(value = {"classpath:application.properties", "file:///ifeng/comic/config/core.properties"},encoding="utf-8")
public class TestController {

    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    Logger logger = loggerContext.getLogger(TestController.class);
    @Autowired
    MatrixCache matrixCache;

    @Value("${dubbo.protocol.zookeeper}")
    String a;

    @Value("${logging.path}")
    String loggingPath;
    @Value("#systemProperties['server.port']")
    String loggingLevelRoot;

    @Value("${comic.version:000}")
    String comicVersion;

    @Autowired
    private Environment env;  // 注入环境变量对象，存储注入的属性值

    @PostConstruct
    public void init() {
        logger.debug("loggingPath:", loggingPath);
        logger.debug("loggingLevelRoot:", loggingLevelRoot);
        logger.debug("build.version:", env.getProperty("build.version"));

    }

    @RequestMapping(value = {"/getCache"}, method = RequestMethod.GET)
    public ResponseEntity getCache(String prefix, String key) {
        return new ResponseEntity(matrixCache.getCache(prefix, key.split(":"), 100, 100), HttpStatus.OK);
    }

    @RequestMapping(value = {"/addCache"}, method = RequestMethod.GET)
    @MatrixAddCache(prefix = "data_is_on_platform_charge", key = "#key")
    @ResponseBody
    public String addCache(String prefix, String key, String value) {
        return value;
    }

    @RequestMapping(value = {"/delCache"}, method = RequestMethod.GET)
    public ResponseEntity delCache(String prefix, String key) {
        String result = "Failed";
        if (matrixCache.exists(prefix, key.split(":"))) {
            result = "OK";
            matrixCache.delCache(prefix, key.split(":"));
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = {"/add_property"}, method = RequestMethod.GET)
    public ResponseEntity addProperty(String key, String value) {
        System.setProperty(key, value);
//        propertyPlaceholderConfigurer.setProperties();
        return new ResponseEntity("", HttpStatus.OK);
    }

    @RequestMapping(value = {"exception"}, method = RequestMethod.GET)
    public ResponseEntity exception(CacheParam param) throws Exception {
        throw new Exception("exception");
    }


    /**
     * 动态设置日志级别
     *
     * @param name
     * @param level
     * @return
     */
    @RequestMapping(value = {"/set_log_level"}, method = RequestMethod.GET)
    public ResponseEntity setLogLevel(String name, String level) {
        Logger logger = loggerContext.getLogger(name);
        logger.setLevel(Level.toLevel(level));
        return new ResponseEntity("", HttpStatus.OK);
    }


//    @Scheduled(cron = "*/4 * * * * *")
    public void run() {
        logger.trace("trace");
        logger.debug("debug");
        logger.warn("warn");
        logger.info("info");
        logger.error("error");
    }
}
